#include "widget.h"
#include "ui_widget.h"
#include <QTreeWidgetItem>
#include <QtDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);


    QTreeWidgetItem* item1 = new QTreeWidgetItem();
    item1->setText(0,"主武器");
    ui->treeWidget->addTopLevelItem(item1);
    //添加子节点
    QTreeWidgetItem* child1 = new QTreeWidgetItem();
    item1->addChild(child1);
    QTreeWidgetItem* child2 = new QTreeWidgetItem();
    item1->addChild(child2);
    QTreeWidgetItem* child3 = new QTreeWidgetItem();
    item1->addChild(child3);
    child1->setText(0,"M4A1-仲达");
    child2->setText(0,"AK47-奉先");
    child3->setText(0,"81-瑜");

    QTreeWidgetItem* item2 = new QTreeWidgetItem();
    item2->setText(0,"副武器");
    ui->treeWidget->addTopLevelItem(item2);
    // 添加子节点
    QTreeWidgetItem* child4 = new QTreeWidgetItem();
    item2->addChild(child4);
    QTreeWidgetItem* child5 = new QTreeWidgetItem();
    item2->addChild(child5);
    QTreeWidgetItem* child6 = new QTreeWidgetItem();
    item2->addChild(child6);
    child4->setText(0,"柯尔特-仲达");
    child5->setText(0,"COP-雷霆王者");
    child6->setText(0,"COP-星象");

    QTreeWidgetItem* item3 = new QTreeWidgetItem(2);
    item3->setText(0,"近战武器");
    ui->treeWidget->addTopLevelItem(item3);
    // 添加子节点
    QTreeWidgetItem* child7 = new QTreeWidgetItem();
    item3->addChild(child7);
    QTreeWidgetItem* child8 = new QTreeWidgetItem();
    item3->addChild(child8);
    QTreeWidgetItem* child9 = new QTreeWidgetItem();
    item3->addChild(child9);
    child7->setText(0,"王者之刺");
    child8->setText(0,"方天画戟");
    child9->setText(0,"雌雄双股剑");

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_add_clicked()
{
    QTreeWidgetItem* item = ui->treeWidget->currentItem();
    if(item == nullptr)
        return;
    QString text = ui->lineEdit->text();
    if(text == "")
        return;
    QTreeWidgetItem* child = new QTreeWidgetItem(item);
    child->setText(0,text);
    ui->lineEdit->setText("");
}

void Widget::on_pushButton_addtop_clicked()
{
    QString text = ui->lineEdit->text();
    if(text == "")
        return;
    QTreeWidgetItem* top = new QTreeWidgetItem();
    ui->treeWidget->addTopLevelItem(top);
    top->setText(0,text);
    ui->lineEdit->setText("");
}

void Widget::on_pushButton_remove_clicked()
{
    QTreeWidgetItem* item = ui->treeWidget->currentItem();
    if(item == nullptr)
        return;
    QTreeWidgetItem* parent = item->parent();
    if(parent == nullptr)
        ui->treeWidget->takeTopLevelItem(ui->treeWidget->indexOfTopLevelItem(item));
    else
        parent->removeChild(item);
    qDebug() << "remove ";
}
